package liy.demo.wikiproject.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import liy.demo.wikiproject.domain.Category;
import liy.demo.wikiproject.mapper.CategoryMapper;
import liy.demo.wikiproject.req.CategoryQueryReq;
import liy.demo.wikiproject.req.CategorySaveReq;
import liy.demo.wikiproject.resp.CommonResp;
import liy.demo.wikiproject.resp.CategoryResp;
import liy.demo.wikiproject.resp.PageResp;
import liy.demo.wikiproject.util.CopyUtil;
import liy.demo.wikiproject.util.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CategoryService {

    @Resource
    CategoryMapper CategoryMapper;

    @Resource
    SnowFlake snowFlake;
    @Cacheable(value = "category",key="'all'")
    public List<CategoryResp> all( ) {
        List<Category> categories = CategoryMapper.selectAll();
        List<CategoryResp> respList = CopyUtil.copyList(categories, CategoryResp.class);
        return buildTree(respList);
    }


    private List<CategoryResp> buildTree(List<CategoryResp> categories) {
        List<CategoryResp> rootCategories = categories.stream()
                .filter(category->category.getParent()==0)
                .collect(Collectors.toList());

        for(CategoryResp rootCategory: rootCategories) {
            buildSubTree(rootCategory,categories);
        }
        return   rootCategories;
    }
    private void buildSubTree(CategoryResp parent, List<CategoryResp> categories) {

        List<CategoryResp> children = categories.stream()
                .filter(category-> Objects.equals(category.getParent(), parent.getId()))
                .collect(Collectors.toList());
        parent.setChildren(children);
        for (CategoryResp child : children) {
            buildSubTree(child, categories);
        }
    }

    //搜索时候使用
    public PageResp<CategoryResp> list( CategoryQueryReq req) {
        if (req.getSize() != null) {
            PageHelper.startPage(req.getPage(), req.getSize());
        }
        List<Category> categoryList;
        if (req.getName() != null && !req.getName().isEmpty()) {
            categoryList = CategoryMapper.selectByName("%" + req.getName() + "%");
        } else {
            categoryList = CategoryMapper.selectAll();
        }
        List<CategoryResp> respList = CopyUtil.copyList(categoryList, CategoryResp.class);
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        PageResp<CategoryResp> pageResp = new PageResp<>();
        pageResp.setTotal((int) pageInfo.getTotal());
        pageResp.setList(respList);
        return pageResp;
    }


    public CommonResp<String> delete(CategoryQueryReq req) {
        long id = req.getId();
        CommonResp<String>commonResp = new CommonResp<>();

        try {
            CategoryMapper.deleteByPrimaryKey(id);
            commonResp.setContent("删除成功");
        }catch (Exception e) {
            commonResp.setContent("删除失败");
        }finally {
            return commonResp;
        }
    }
    public void save(CategorySaveReq req) {
        Category category = CopyUtil.copy(req,Category.class);
        if(ObjectUtils.isEmpty(req.getId())) {
            long l = snowFlake.nextId();
            category.setId(l);
            CategoryMapper.insert(category);
        }else{
            CategoryMapper.updateByPrimaryKey(category);

        }
    }
}
